package com.jcgyl.export.runner;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.jcgyl.export.dao.TbbciqunitDao;
import com.jcgyl.export.entity.Tbgoodsinfo;
import com.jcgyl.export.model.SystemConstants;
import com.jcgyl.export.service.RedisService;

@Component
@Order(2)
public class RedisRunner implements CommandLineRunner {

	@Value("${spring.datasource.url}")
	private String server;
	@Value("${spring.datasource.username}")
	private String user;
	@Value("${spring.datasource.password}")
	private String password;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private RedisService redisService;
	@Autowired
	private TbbciqunitDao tbbciqunitDao;

	@Override
	public void run(String... args) throws Exception {
		System.out.println(">>>>>>>>>>>>>>>数据缓存加载开始执行...<<<<<<<<<<<<<");
		ListOperations operations = redisTemplate.opsForList();
		//
		// List<Tbbciqunit> tbbciqunits = tbbciqunitDao.findAllTbbciqunit();
		// // operations.leftPush("export.oms.cus_unit", tbbciqunits);
		// redisService.lPush("export.oms.cus_unit", tbbciqunits);
		// // List<Tbbciqunit> resultList1 = (List<Tbbciqunit>)
		// // redisTemplate.opsForList().leftPop("cus_unit");
		// // System.out.println("resultList1:" + resultList1);
		// String sql = "select * from goodsinfo";
		// Sql2o sql2o = MyDao.getInstance(server, user, password);
		// final int BATCH_SIZE = 1000;
		//
		// List<Tbgoodsinfo> batch = new ArrayList<Tbgoodsinfo>(BATCH_SIZE);
		//
		// try (Connection con = sql2o.open()) {
		// try (ResultSetIterable<Tbgoodsinfo> tasks =
		// con.createQuery(sql).executeAndFetchLazy(Tbgoodsinfo.class)) {
		// for (Tbgoodsinfo task : tasks) {
		// if (batch.size() == BATCH_SIZE) {
		// InitGoodsInfoToRedis(batch);
		// batch.clear();
		// }
		// batch.add(task);
		// }
		// }
		// }
		// InitGoodsInfoToRedis(batch);
	}

	private void InitGoodsInfoToRedis(List<Tbgoodsinfo> batch) {
		for (Tbgoodsinfo goods : batch) {
			redisService.set(SystemConstants.GoodsInfo_Prefix + ":" + goods.getGoodsname(), goods);
		}
	}
}
